﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using Telerik.Web.UI;
using System.Collections;
using NCC_SOP.app_codes;


namespace NCC_SOP.Admin
{
    public partial class manageUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initBind();
            }
        }

        protected void initBind()
        {
            ManageUserID.DataSource = getAllUserNames();
            ManageUserID.DataBind();
        }

        protected DataSet getAllUserNames()
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                dt = ds.Tables.Add("Users");
                dt.Columns.Add("UserName", Type.GetType("System.String"));
                MembershipUserCollection muc = Membership.GetAllUsers();
                foreach (MembershipUser mu in muc)
                {
                    DataRow dr;
                    dr = dt.NewRow();
                    dr["UserName"] = mu.UserName;
                    dt.Rows.Add(dr);
                }
            }
            catch (Exception e)
            {
                Session["ErrorMessage"] = e.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
            return ds;
        }

        protected void ManageUserID_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = (GridEditableItem)e.Item;
                string userName = item.OwnerTableView.Items[e.Item.ItemIndex]["UserName"].Text;
                Session["UserName"] = userName;
                Response.Redirect("~/Admin/AdministratorOnly/editUser.aspx", false);
            }
            catch (Exception e1)
            {
                Session["ErrorMessage"] = e1.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }

        protected void ManageUserID_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            
                dbConnection db = new dbConnection();
                GridEditableItem item = (GridEditableItem)e.Item;
                string userName = item.OwnerTableView.Items[e.Item.ItemIndex]["UserName"].Text;
                string[] roles = db.getReportRolesForUser(userName);
                db.removeUserFromReportRoles(userName, roles);
                Membership.DeleteUser(userName);
                initBind();
            
        }

        protected CheckBoxList checkUserRolesInCheckBox(CheckBoxList cbl, string userName)
        {
            try
            {
                string[] roles = Roles.GetAllRoles();
                cbl.DataSource = roles;
                cbl.DataBind();
                string[] userRoles = Roles.GetRolesForUser(userName);
                foreach (string role in userRoles)
                {
                    foreach (ListItem li in cbl.Items)
                    {
                        if (role.Equals(li.Value))
                        {
                            li.Selected = true;
                        }
                    }
                }
            }
            catch (Exception e3)
            {
                Session["ErrorMessage"] = e3.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
            return cbl;
        }
    }
}
